package com.zt.dev.processMonitoring.controller;

import com.zt.dev.common.ServerResponse;
import com.zt.dev.processMonitoring.pojo.ProcessMonitoring;
import com.zt.dev.processMonitoring.pojo.TestNode;
import com.zt.dev.processMonitoring.service.ProcessMonitoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 卜德凯
 * @date 2020/2/25 18:39
 */
@RestController
@RequestMapping("/pm")
public class ProcessMonitoringController {

    @Autowired
    private ProcessMonitoringService processMonitoringService;


    /**
     * 添加预期进度
     * @param expectedPlan
     * @return
     */
    @RequestMapping("/expectedPlan")
    public ServerResponse insertExpectedPlan(String expectedPlan){
        Integer result = processMonitoringService.insertExpectedPlan(expectedPlan);
        return ServerResponse.createBySuccessMessage("实验进度更新成功！");
    }


    /**
     * 更新实验进度
     * @param experimentalPlan
     * @return
     */
    @RequestMapping("/updateExperimentalPlan")
    public ServerResponse updateExperimentalPlan(String experimentalPlan,Integer id) {
        if (!StringUtils.isEmpty(experimentalPlan) && id != null) {
            //根据id获取数据库已有的实验数据
            ProcessMonitoring processMonitorin = processMonitoringService.findExperimentalPlanById(id);
            //设置日期格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // new Date()为获取当前系统时间
            String completionTime = df.format(new Date());
            if (!StringUtils.isEmpty(processMonitorin.getExperimentalPlan())) {
                //newExperimentalPlan为新的实验进度完成时间
                String newExperimentalPlan = processMonitorin.getExperimentalPlan() + "," + experimentalPlan;
                //数据库中已有数据，在已有的实验进度、实验完成时间的基础上，加入新的实验进度、实验完成时间
                Integer result = processMonitoringService.updateExperimentalPlan(newExperimentalPlan,"," + completionTime,id);
                if (result > 0) {
                    return ServerResponse.createBySuccessMessage("实验进度更新成功！");
                }
            }
            //数据库中没有实验进度直接加入新的实验进度
            Integer result = processMonitoringService.updateExperimentalPlan(experimentalPlan,completionTime,id);
            if (result > 0) {
                return ServerResponse.createBySuccessMessage("实验进度更新成功！");
            }
            return ServerResponse.createByErrorMessage("实验进度更新失败！");
        }
        return ServerResponse.createByErrorMessage("参数有误！");
    }

    /**
     * 添加节点
     * @param testNodeList
     * @return
     */
    @RequestMapping("/addTestNode")
    public ServerResponse addTestNode(@RequestBody List<TestNode> testNodeList){
        if(!StringUtils.isEmpty(testNodeList)){
            Integer result = processMonitoringService.addTestNode(testNodeList);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("节点添加成功！");
            }
            return ServerResponse.createByErrorMessage("节点添加失败！");
        }
        return ServerResponse.createByErrorMessage("节点数据有误！");
    }

    /**
     * 1.根据id修改节点状态
     * private Integer status;//节点状态（每个节点的）（0未开始、1执行中、2已完成）
     * 2.根据id修改节点的实际完成时间和完成情况描述
     * private String actualEndTime;//实际完成时间（每个节点的）
     * private String describe;//完成情况描述（每个节点的）
     * @param testNode
     * @return
     */
    @RequestMapping("/updateNodeState")
    public ServerResponse updateNodeState(TestNode testNode){
        if(!StringUtils.isEmpty(testNode)){
            Integer result = processMonitoringService.updateNodeState(testNode);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("成功！");
            }
            return ServerResponse.createByErrorMessage("失败！");
        }
        return ServerResponse.createByErrorMessage("数据有误！");
    }

    /**
     * 获得所有已有进程的试验
     * @return
     */
    @RequestMapping("/findPageTPM")
    public ServerResponse findPageTPM(){
        List<ProcessMonitoring> pageTPM = processMonitoringService.findPageTPM();
        if(!StringUtils.isEmpty(pageTPM)){
            return ServerResponse.createBySuccess(pageTPM);
        }
        return ServerResponse.createByErrorMessage("获取试验数据失败！");
    }

    /**
     * 根据试验id查看具体进程
     * @param pId
     * @return
     */
    @RequestMapping("/viewProcess")
    public ServerResponse viewProcess(Integer pId){
        if(pId != 0 && pId != null){
            List<TestNode> testNodeList = processMonitoringService.viewProcess(pId);
            if(!StringUtils.isEmpty(testNodeList)){
                return ServerResponse.createBySuccess(testNodeList);
            }
            return ServerResponse.createByErrorMessage("失败！");
        }
        return ServerResponse.createByErrorMessage("参数有误！");
    }


    public double findPercentage(Integer pId){
        //根据pId获得一条试验的节点个数
        List<TestNode> testNodeList = processMonitoringService.viewProcess(pId);
        int count = testNodeList.size();
        int completeCount = 0;
        for(TestNode testNode : testNodeList){
            if(testNode.getStatus() == 3){
                completeCount++;
            }
        }
        double  percentage =  (completeCount / count * 100);
        Math.round(percentage);
        return percentage;
    }
    /**
     * 复制试验节点
     * @return
     */
    @RequestMapping("/copyTestNode")
    public ServerResponse copyTestNode(Integer pId,Integer status,Integer id){
        if(pId != 0 && pId != null){
            List<TestNode> testNodeList = processMonitoringService.viewProcess(pId);
            if(!StringUtils.isEmpty(testNodeList)){
                ArrayList list = new ArrayList();
                if(status == 0){//复制时保留计划完成时间
                    for (TestNode testNode: testNodeList) {
                        testNode.setActualEndTime(null);
                        testNode.setDescribe(null);
                        testNode.setStatus(0);
                        testNode.setPId(id);
                        list.add(testNode);
                    }
                }else if(status == 1){//复制时不保留计划完成时间
                    for (TestNode testNode: testNodeList) {
                        testNode.setPlannedEndTime(null);
                        testNode.setActualEndTime(null);
                        testNode.setDescribe(null);
                        testNode.setStatus(0);
                        testNode.setPId(id);
                        list.add(testNode);
                    }
                }
                if(!StringUtils.isEmpty(list)){
                    Integer result = processMonitoringService.addTestNode(list);
                    if(result > 0){
                        return ServerResponse.createBySuccessMessage("复制成功！");
                    }
                }
                return ServerResponse.createByErrorMessage("复制失败！");
            }
            return ServerResponse.createByErrorMessage("未查到要复制的数据！");
        }
        return ServerResponse.createByErrorMessage("参数有误！");
    }
}
